//
//  USCommentCell.m
//  us
//
//  Created by karry on 2025/5/22.
//

#import "USCommentCell.h"

@interface USCommentCell ()

@property(nonatomic,strong) UILabel *nameLab;
@property(nonatomic,strong) UILabel *timeLab;

@property(nonatomic,strong) UIView *messageContentView;
@property(nonatomic,strong) UILabel *contentLab;

@end

@implementation USCommentCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupView];
    }
    return self;
}

- (void)setModel:(USCommentModel *)model {
    _model = model;
    
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:model.gmtCreate];
    NSString *dayStr = [date jk_stringWithFormat:@"yyyy-MM-dd HH:mm:ss"];
    self.timeLab.text = dayStr;
//    self.contentLab.text = model.content;
    
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.minimumLineHeight = 18;
    paragraphStyle.maximumLineHeight = 18;
    NSDictionary *attributes = @{
        NSParagraphStyleAttributeName: paragraphStyle,
    };

    NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:model.content?:@"" attributes:attributes];
    self.contentLab.attributedText = attributedText;
    

    
    if ([model.userId isEqualToString: USUser.user.userId]) {
        self.nameLab.text = USUser.user.nickname;
        
        self.nameLab.textAlignment = NSTextAlignmentRight;
        self.timeLab.textAlignment = NSTextAlignmentRight;
 
        [self.messageContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.timeLab.mas_bottom).offset(16);
            make.right.mas_equalTo(self.nameLab);
            make.bottom.mas_equalTo(self.contentView.mas_bottom).offset(-16).priorityLow(); // ✅
            make.width.mas_greaterThanOrEqualTo(30).priority(250);
            make.left.mas_greaterThanOrEqualTo(80).priority(250);
            make.height.mas_greaterThanOrEqualTo(30).priority(250);
        }];
    }else{
        self.nameLab.text = USUser.user.relUser.nickname;

        self.nameLab.textAlignment = NSTextAlignmentLeft;
        self.timeLab.textAlignment = NSTextAlignmentLeft;
 
        [self.messageContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.timeLab.mas_bottom).offset(16);
            make.left.mas_equalTo(self.nameLab);
            make.bottom.mas_equalTo(self.contentView.mas_bottom).offset(-16).priorityLow(); // ✅
            make.width.mas_greaterThanOrEqualTo(30).priority(250);
            make.left.mas_greaterThanOrEqualTo(80).priority(250);
            make.height.mas_greaterThanOrEqualTo(30).priority(250);
        }];
    }
  

}

- (void)setupView{
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    self.backgroundColor = UIColor.clearColor;
    self.contentView.backgroundColor = UIColor.clearColor;
    
    self.nameLab = [UILabel new];
    self.nameLab.font = [UIFont systemFontOfSize:16];
    self.nameLab.textColor = UIColor.labelColor;
    self.nameLab.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:self.nameLab];
    [self.nameLab mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(24);
        make.right.mas_offset(-24);
        make.top.mas_offset(16);
        make.height.mas_equalTo(18);
    }];
    
    self.timeLab = [UILabel new];
    self.timeLab.font = [UIFont systemFontOfSize:12];
    self.timeLab.textColor = [UIColor jk_colorWithHex:0x212121 andAlpha:0.2];
    self.timeLab.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:self.timeLab];
    [self.timeLab mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.nameLab);
        make.top.mas_equalTo(self.nameLab.mas_bottom).offset(4);
        make.height.mas_equalTo(12);
    }];
    
    self.messageContentView = [UIView new];
    self.messageContentView.layer.cornerRadius = 16;
    self.messageContentView.layer.masksToBounds = YES;
    self.messageContentView.backgroundColor = UIColor.mi_2_color;
    [self.contentView addSubview:self.messageContentView];
    [self.messageContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.timeLab.mas_bottom).offset(16);
        make.right.mas_equalTo(self.nameLab);
        make.bottom.mas_equalTo(self.contentView.mas_bottom).offset(-16).priorityLow(); // ✅
        make.width.mas_greaterThanOrEqualTo(30).priority(250);
        make.left.mas_greaterThanOrEqualTo(80).priority(250);
        make.height.mas_greaterThanOrEqualTo(30).priority(250);
    }];
    
    self.contentLab = [UILabel new];
    self.contentLab.font = [UIFont systemFontOfSize:14];
    self.contentLab.textColor = UIColor.labelColor;
    self.contentLab.textAlignment = NSTextAlignmentLeft;
    self.contentLab.numberOfLines = 0;
    [self.messageContentView addSubview:self.contentLab];
    [self.contentLab mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.left.mas_equalTo(16);
        make.right.mas_equalTo(-16);
        make.bottom.mas_equalTo(-10);
        make.height.mas_greaterThanOrEqualTo(20);
        make.width.mas_lessThanOrEqualTo(250);
    }];
    
 
         
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
